import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @author zjq
 * @version 1.0
 * @date 2021/1/31 16:52
 */
public class killPort {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要查询的端口号：");
        int port = sc.nextInt();// 要查询的端口号

        killPort killPort = new killPort();
        String pid = killPort.getPID(String.valueOf(port));//获取pid == 135
        System.out.println("端口号为:" + port + "的PID 为：" + pid);
        if (pid==null){
            System.out.println("该端口下没有进程！！！");
            System.exit(0);
        }else {
            String pressName = killPort.getPressName(pid);
            System.out.println("进程名为:" + pressName);
            System.out.println("你要关闭该进程吗？？？  （0/1）");
            int i = sc.nextInt();
            if (i == 1) {
                System.out.println("准备关闭 " + "pressName" + " 进程！！！");
                killPort.killProcess(pid);
            } else {
                System.exit(0);//退出程序
            }
        }
    }

    /**
     * 结束进程
     */
    public void killProcess(String pid) {
        Runtime runtime = Runtime.getRuntime();
        try {
            //taskkill /pid 10580 -f
            Process tasklist = runtime.exec("taskkill /pid " + pid + " -f");
            System.out.println("进程关闭成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 拿到PID对应的进程名
     * @return
     */
    public String getPressName(String PID) {
        StringUtils stringUtils = new StringUtils();
        Runtime runtime = Runtime.getRuntime();
        String processName = null;
        int index = 0;
        try {
            Process tasklist = runtime.exec("tasklist");
            BufferedReader reader = new BufferedReader(new InputStreamReader(tasklist.getInputStream(), "GBK"));
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                if (index >= 3) {
//                    System.out.println(line);
                    String findpid = stringUtils.findpid(line);
                    if (PID.equals(findpid)) {
                        processName = stringUtils.getProcessName(line);
                    }
                }
                index++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return processName;
    }

    /**
     * 返回端口号对应的PID
     *
     * @param Port
     * @return
     */
    public String getPID(String Port) {
        StringUtils stringUtils = new StringUtils();
        Runtime runtime = Runtime.getRuntime();
        String PID = null;
        try {
            Process findPort = runtime.exec("netstat -ano");
            BufferedReader reader = new BufferedReader(new InputStreamReader(findPort.getInputStream(), "GBK"));
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
//                System.out.println(line);
                String ipAndPort = stringUtils.findIPAndPort(line);
                String port = stringUtils.findPort(ipAndPort);
                if (port.equals(Port)) {
                    PID = stringUtils.findPID(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return PID;
    }
}
